package com.javasearch.search;

import com.javasearch.Config;
import com.javasearch.index.IndexEnum;
import com.javasearch.index.build.ClassBuild;
import com.javasearch.utils.FileUtil;
import com.javasearch.utils.ReadFileLineUtil;
import com.javasearch.utils.WriteFileStrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class ClassSearch  {
    private static final Logger logger = LoggerFactory.getLogger(ClassSearch.class);
    private static final String CLASSPATH = FileUtil.getTempDirectoryPath() + IndexEnum.CLASS.getIndexName() ;
    private static final String FIELDPATH = FileUtil.getTempDirectoryPath() + IndexEnum.FIELD.getIndexName() ;
    private static final String METHODPATH = FileUtil.getTempDirectoryPath() + IndexEnum.METHOD.getIndexName() ;
    private static final String ANNOTATIONPATH = FileUtil.getTempDirectoryPath() + IndexEnum.ANNOTATION.getIndexName() ;
    public static void classNameSearch( String keyWord, StringBuffer sb, Config config) {
        File file = ClassBuild.hashId(keyWord, 100, IndexEnum.CLASS);
        if (!file.exists()) {
            return;
        }
        nameSearch(file, keyWord, sb, config);
    }

    public static void fieldNameSearch( String keyWord, StringBuffer sb, Config config) {
        File file = ClassBuild.hashId(keyWord, 500, IndexEnum.FIELD);
        if (!file.exists()) {
            return;
        }
        nameSearch(file, keyWord, sb, config);

    }
    public static void methodNameSearch( String keyWord, StringBuffer sb, Config config) {
        File file = ClassBuild.hashId(keyWord, 500, IndexEnum.METHOD);
        if (!file.exists()) {
            return;
        }
        nameSearch(file, keyWord, sb, config);

    }
    public static void annotationNameSearch( String keyWord, StringBuffer sb, Config config) {
        File file = ClassBuild.hashId(keyWord, 500, IndexEnum.ANNOTATION);
        if (!file.exists()) {
            return;
        }
        nameSearch(file, keyWord, sb, config);

    }
    public static void nameSearch( File file, String keyWord, StringBuffer sb, Config config) {
        try {
            ReadFileLineUtil.readFileStrLine(file, (line) -> {
                //切割##
                String[] split = line.split("##");
                //获取第二个,去掉[]
                String key = split[1].substring(1, split[1].length() - 1);
                if (key.equals(keyWord)) {
                    synchronized (config) {
                        Integer searchLimit = config.getSearch().getSearchLimit();
                        if (searchLimit == 0) {
                            throw new SearchMaxLimitException("ClassSearch搜索已达到上限");
                        }
                        config.getSearch().setSearchLimit(searchLimit - 1);
                    }
                    sb.append(line).append("\n");
                }
            });
        } catch (SearchMaxLimitException e) {

        }
    }

    public static void search(Config config) {
        String searchPath = config.getSearch().getSearchPath();
        StringBuffer stringBuffer = new StringBuffer();
        classNameSearch(config.getSearch().getSearchKey(),stringBuffer, config);
        fieldNameSearch(config.getSearch().getSearchKey(),stringBuffer, config);
        methodNameSearch(config.getSearch().getSearchKey(),stringBuffer, config);
        annotationNameSearch(config.getSearch().getSearchKey(),stringBuffer, config);
        //将搜索结果写入文件
        WriteFileStrUtil.writeStrLock(new File(searchPath), stringBuffer.toString(), true);
        logger.info("ClassSearch-搜索结果:\n{}", stringBuffer);
    }
}
